package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.entity.Role;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色管理
     * 1、搭架子
     * 2、补参数
     * 3、写方法
     */
    @GetMapping
    public Result page(@RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "label", required = false) String label,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
        PageResult<Role> pageResult = roleService.page(name, label, page, pageSize);
        log.info("分页查询角色：{}", pageResult);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result listRole() {
        log.info("查询所有角色");
        List<Role> list = roleService.listRole();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer roleid) {
        log.info("根据ID查询角色：{}", roleid);
        Role role = roleService.selectById(roleid);
        return Result.success(role);
    }

    @PutMapping
    @LogOperation
    public Result updateRole(@RequestBody Role role) {
        log.info("修改角色：{}", role);
        roleService.updateRole(role);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @LogOperation
    public Result deleteById(@PathVariable("id") Integer roleid) {
        log.info("删除角色：{}", roleid);
        roleService.deleteById(roleid);
        return Result.success();
    }

    @PostMapping
    @LogOperation
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色：{}", role);
        roleService.addRole(role);
        return Result.success();
    }


}
